package com.qidian.mes.qc.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.pojo.Index;
import com.qidian.mes.qc.mapper.IndexMapper;
import com.qidian.mes.qc.service.IndexService;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexMapper indexMapper;
    @Override
    public MESResult queryById(Long indexId) {
        Index index=null;
        if ((index=indexMapper.selectById(indexId))!=null){
            return MESResult.ok(index);
        }
        return MESResult.empty();
    }

    @Override
    public MESResult list(int page, int limit, Index index) {
        // 开启分页 在当前线程中执行下一条SQL的时候就会进行分页。
        PageHelper.startPage(page,limit);
        List<Index> list =indexMapper.list(index);
        // 组织一个分页对象。（页码，每页大小，最大页码，数据，总条数）
        PageInfo<Index> pageInfo=new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }

    @Override
    public MESResult save(Index index) {
        //查重
        if (indexMapper.selectByIndexName(index.getIndexName())!=null ){
            return MESResult.duplicate("缺陷名重复");
        }
        indexMapper.save(index);
        return MESResult.ok();
    }

    @Override
    public MESResult update(Index index) {
        //校验是否重复
        Index temp=indexMapper.selectByIndexName(index.getIndexName());
        if(temp !=null && ! temp.getIndexId().equals(index.getIndexId())){
            return  MESResult.duplicate("检测项重复");
        }
        indexMapper.update(index);
        return MESResult.ok();
    }

    @Override
    public MESResult deleteByIndexId(Long indexId) {

        indexMapper.deleteByIndexId(indexId);
        return MESResult.ok();
    }

    @Override
    public MESResult deletesByIndexIds(Long[] indexId) {
        int  count = indexMapper.deletesByIndexId(indexId);
        return  MESResult.ok(count+"");
    }


}
